
	// == a checkbox has been clicked ==
    function boxclick(box, name) {
    	      if (box.checked) {
    	          show();
    	        } else {
    	          hide();
    	        }
    	        // == rebuild the side bar
    	        makeSidebar();
    }	
    

    function showOtherPolygon() {
    	if(other_polygon.length > 0) {
            for (var i=0; i<other_polygon.length; i++) {
    			other_polygon[i].show();
    		}            
            return true;
        }
    	
    	return false;
    	
    }

    function hideOtherPolygon() {
        for (var i=0; i<other_polygon.length; i++) {
			other_polygon[i].hide();
		}
    }

    // == shows all markers of a particular category, and ensures the checkbox is checked ==
    function show() {
      for (var i=0; i<gmarkers.length; i++) {
          gmarkers[i].show();
      }
    }
    
    // == hides all markers of a particular category, and ensures the checkbox is cleared ==
    function hide() {
      for (var i=0; i<gmarkers.length; i++) {
          gmarkers[i].hide();
      }
      // == clear the checkbox ==
      //document.getElementById(category+"box").checked = false;
      // == close the info window, in case its open on a marker that we just hid
      //map.closeInfoWindow();
    }      
    
    function clearMarkers() {
//        for (var i=0; i<gmarkers.length; i++) {
//            map.removeOverlay(gmarkers[i]);
//        }   
    	// remove all overlay, including the other layers
    	// should update to clear only the polygon we draw
    	map.clearOverlays();
        gmarkers = [];
        other_polygon = [];
    }
    
    // == rebuilds the sidebar to match the markers currently displayed ==
    function makeSidebar() {
      var html = "";
      for (var i=0; i<gmarkers.length; i++) {
        if (!gmarkers[i].isHidden()) {
          html += '<a href="javascript:myclick(' + i + ')">' + gmarkers[i].getLatLng() + '<\/a><br>';
        }
      }
      document.getElementById("side_bar").innerHTML = html;
    }
   
    function createPolygon() {
        var polyPoints = [];
        for (var i=0; i<gmarkers.length; i++) {
			polyPoints[i] = gmarkers[i].getLatLng();
        }  
        // closing the polygon
        polyPoints[polyPoints.length] = gmarkers[0].getLatLng();
    	
        var polygon = new GPolygon(polyPoints, "#f33f00", 2, 1, "#ff0000", 0.2);
    	map.addOverlay(polygon);
        document.mapForm.hid.value = createMarkerData(gmarkers);
	}
    
    function showPolygon(areaMarkerData) {
        for (var i=0; i<areaMarkerData.length; i++) {
        	buildAPolygon(areaMarkerData[i]);
        }

    }
    
    function buildAPolygon(areaJSON) {
    	var markerData = areaJSON.markers;
    	var polyData = [];
        for (var i=0; i<markerData.length; i=i+2) {
        	var latLng= new GLatLng(markerData[i], markerData[i+1]);
        	polyData.push(latLng);
        }
        polyData.push(new GLatLng(markerData[0], markerData[1]));
    	var polygon = new GPolygon(polyData, areaJSON.defaultAreaColor, 2, 1, areaJSON.defaultLineColor, 0.2);
    	map.addOverlay(polygon);
    	other_polygon.push(polygon);
    }
    
    function createMarkerData(gmarkers) {
    	var markerData;
        for (var i=0; i<gmarkers.length; i++) {
			markerData += gmarkers[i].getLatLng() + ":";
        }  
        
        return markerData;
    }
